<template>
  <div class="page-registet">
    <article class="header">
      <header>
        <a href="/" class="site-logo"/>
        <span class="login">
          <em class="blod">已有美团账号?</em>
          <a href="/login">
            <el-button class="button" type="primary" size="small">登录</el-button>
          </a>
        </span>
      </header>
    </article>
    <div class="section">
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="昵称" prop="name">
          <el-input v-model="ruleForm.name"/>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input type="email" v-model="ruleForm.email"/>
          <el-button class="button" size="mini" round @click="sendMsg">发送验证码</el-button>
          <span class="status">{{ statusMsg }}</span>
        </el-form-item>
        <el-form-item label="验证码" prop="code">
          <el-input v-model="ruleForm.code" maxlength="4"/>
        </el-form-item>
        <el-form-item label="密码" prop="pwd">
          <el-input type="password" v-model="ruleForm.pwd"/>
        </el-form-item>
        <el-form-item label="确认密码" prop="cpwd">
          <el-input type="password" v-model="ruleForm.cpwd"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="register">同意以下协议并注册</el-button>
          <div class="error">{{error}}</div>
        </el-form-item>
        <el-form-item>
          <a href="http://www.meituan.com/about/terms" target="_blank">《美团网用户协议》</a>
        </el-form-item>
      </el-form>
      <span :class="{'to': to,'hide': hide}">{{time}}秒后跳转,&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="jump" @click="jump">点此跳转登录</span></span>
    </div>
  </div>
</template>

<script>
import axios from "axios"
import CryptoJS from "crypto-js"
export default {
  data() {
    return {
      to: false,
      hide: true,
      time: 5,
      statusMsg: "",
      error: "",
      ruleForm: {
        name: "",
        email: "",
        pwd: "",
        cpwd: "",
        code: ""
      },
      rules: {
        name: [
          {
            required: true,
            type: "string",
            message: "请输入昵称",
            trigger: "blur"
          }
        ],
        email: [
          {
            required: true,
            type: "email",
            message: "请输入邮箱",
            trigger: "blur"
          }
        ],
        pwd: [
          {
            required: true,
            message: "创建密码",
            trigger: "blur"
          }
        ],
        cpwd: [
          {
            required: true,
            message: "确认密码",
            trigger: "blur"
          },
          {
            validator: (rule, value, callback) => {
              if (value === "") {
                callback(new Error("请再次输入密码"));
              } else if (value !== this.ruleForm.pwd) {
                callback(new Error("两次输入密码不一致"));
              } else {
                callback();
              }
            },
            trigger: "blur"
          }
        ]
      }
    };
  },
  layout: "blank",
  methods: {
    sendMsg: function () {
      const self = this;
      let namePass
      let emailPass
      if (self.timerid) {
        return false
      }
      // 这是element对信息的校验 我们可以在这得到校验结果如果校验成功继续
      // element的校验信息规则是如果namePass存在就证明有错误为空就可以继续向下进行
      this.$refs['ruleForm'].validateField('name', (valid) => {
        namePass = valid
        console.log(namePass)
      })
      self.statusMsg = ''
      if (namePass) {
        return false
      }
      this.$refs['ruleForm'].validateField('email', (valid) => {
        emailPass = valid
      })
      if (!namePass && !emailPass) {
        axios.post('/users/verify', {
          username: encodeURIComponent(self.ruleForm.name),
          email: self.ruleForm.email
        }).then(({
          status,
          data
        }) => {
          if (status === 200 && data && data.code === 0) {
            let count = 60;
            self.statusMsg = `验证码已发送,剩余${count--}秒`
            self.timerid = setInterval(function () {
              self.statusMsg = `验证码已发送,剩余${count--}秒`
              if (count === 0) {
                clearInterval(self.timerid)
                self.statusMsg = ''
              }
            }, 1000)
          } else {
            self.statusMsg = data.msg
          }
        })
      }
    },
    register: function() {
      let self = this;
      this.$refs["ruleForm"].validate((valid) => {
        if (valid) {
          axios.post("/users/signup", {
              username: window.encodeURIComponent(self.ruleForm.name),
              password: CryptoJS.MD5(self.ruleForm.pwd).toString(),
              email: self.ruleForm.email,
              code: self.ruleForm.code
            }).then((res) => {
              if (res.status) {
                if (res.data.code === 0) {
                  this.to = true
                  this.hide = false
                  let time = 5
                  self.timer = setInterval(() => {
                    time --
                    self.time = time
                    if (time === 0) {
                      window.location.href="/login"
                      this.to = false
                      this.hide = true
                      clearInterval(self.timer)
                    }
                  },1000)
                }
                alert(res.data.msg)
              } else {
                alert(`服务器出错,错误码为${res.status}`)
              }
            })
            // .then(({ status, data }) => {
            //   if (status === 200) {
            //     if (data && data.code === 0) {
            //       console.log(data.code)
            //       // location.href = "/login";
            //     } else {
            //       self.error = data.msg;
            //     }
            //   } else {
            //     self.error = `服务器出错,错误码:${status}`;
            //   }
            //   setTimeout(function() {
            //     self.error = "";
            //   }, 1500);
            // });
         }
      });
    },
    jump() {
      window.location.href ="/login"
    }
  }
};
</script>

<style lang="scss">
@import "@/assets/css/register/index.scss";
.header {
  min-width: 980px;
  height: 59.2px;
  border-bottom: 2px solid #2bb8aa;
  header {
    margin: 0 auto;
    padding: 10px 0;
    width: 980px;
    .site-logo {
      display: inline-block;
      width: 128px;
      width: 54px;
      height: 36px;
      text-indent: -9999px;
      background-position: -669px -748px;
      background-image: url(//s0.meituan.net/bs/file/?f=fe-sso-fs:build/assets/sp-normal.2ee5c09.png);
    }

    .login {
      float: right;
      .blod {
        font-style: normal;
      }
      a {
        .button {
          margin-left: 20px;
          font-size: 13px;
          background: #2db3a6;
          border-color: #2db3a6;
          //     display: inline-block;
          //     background: #2db3a6;
          //     font-size: 12px;
          //     width: 46.4px;
          //     line-height: 24.6px;
          //     height: 24.6px;
          //     border-radius: 0;
          //     text-align: center;
        }
      }
    }
  }
}
.section {
  margin: 0 auto;
  padding-top: 30px;
  width: 980px;
  min-height: 300px;
  box-sizing: border-box;
  padding-right: 550px;
  .button {
    margin-top: 10px;
  }
  .status {
    font-size: 12px;
    margin-left: 20px;
    color: #e6a23c;
  }
  .error {
    color: red;
  }
  .to {
    font-size: 15px;
    margin-left: 50px;
    color: #e6a23c;
    .jump {
      font-size: 18px;
      color: #aaa; 
      cursor: pointer;
    }
  }
  .hide {
    display: none;
  }
}
</style>


